using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    public AudioSource BGMSource;
    public AudioSource SESource;
    public float BGM1FadeInTime = 0.5f;
    public float BGM1FadeOutTime = 1.0f;
    public float BGM2FadeInTime = 5.0f;
    public float BGM2FadeOutTime = 0.5f;
    public string BGM1Name = "";
    public string BGM2Name = "";

    public float BGMVolume = 1.0f;
    public float SEVolume = 1.0f;

    public static AudioManager Instance;

    private AudioSource[] PausedAudios;
    private float CurrentVolume = 1.0f;
    private string NewBGM = "";
    private float FadeInSpeed = 0.0f;
    private float FadeOutSpeed = 0.0f;
    private bool bChangingBGM = false;
    private bool bMuted = false;

    // Start is called before the first frame update
    void Awake()
    {
        // Create Instance
        {
            if (null == Instance)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else if (Instance != this)
            {
                Destroy(gameObject);
                return;
            }
        }

        // Init AudioSource
        {
            BGMSource = gameObject.GetComponent<AudioSource>();

            if (null == BGMSource)
                BGMSource = gameObject.AddComponent<AudioSource>();

            BGMSource.loop = true;

            BGMSource = gameObject.GetComponent<AudioSource>();

            if (null == SESource)
                SESource = gameObject.AddComponent<AudioSource>();
        }
    }

    public void PlayerBGM1()
    {
        PlayBGM(BGM1Name, 1 / BGM2FadeOutTime, 1 / BGM1FadeInTime);
    }

    public void PlayerBGM2()
    {
        PlayBGM(BGM2Name, 1 / BGM1FadeOutTime, 1 / BGM2FadeInTime);
    }

    public void PlayBGM(string InBGMName, float InFadeOutSpeed = 1.0f, float InFadeInSpeed = 1.0f)
    {
        if (null == BGMSource.clip)
        {
            BGMSource.clip = Resources.Load<AudioClip>(string.Format("Audio/{0}", InBGMName));
            BGMSource.Play();
            return;
        }

        if (InBGMName == BGMSource.clip.name)
            return;

        if (bChangingBGM)
        {
            NewBGM = InBGMName;
            FadeInSpeed = InFadeInSpeed;
            return;
        }

        if (1.0f == CurrentVolume)
        {

            NewBGM = InBGMName;
            FadeInSpeed = InFadeInSpeed;
            FadeOutSpeed = InFadeOutSpeed;
            bChangingBGM = true;
            return;
        }

        CurrentVolume = 0.0f;
        BGMSource.clip = Resources.Load<AudioClip>(string.Format("Audio/{0}", InBGMName));
        BGMSource.Play();
        BGMSource.volume = CurrentVolume * (bMuted? 0.0f : BGMVolume);

        NewBGM = "";
    }

    public void PlaySE(string InSEName, float InVolumeModifier = 1.0f)
    {
        string NamePath = string.Format("Audio/{0}", InSEName);
        AudioClip Ac = Resources.Load<AudioClip>(NamePath);

        SESource.PlayOneShot(Ac, (bMuted ? 0.0f : SEVolume) * InVolumeModifier);
    }

    public void PauseAll()
    {
        PausedAudios = FindObjectsOfType<AudioSource>();
        for (int i = 0; i < PausedAudios.Length; i++)
        {
            PausedAudios[i].Pause();
        }
    }

    public void UnPauseAll()
    {
        PausedAudios = FindObjectsOfType<AudioSource>();
        for (int i = 0; i < PausedAudios.Length; i++)
        {
            PausedAudios[i].UnPause();
        }
    }

    public void MuteAll()
    {
        bMuted = true;
        PausedAudios = FindObjectsOfType<AudioSource>();
        for (int i = 0; i < PausedAudios.Length; i++)
        {
            PausedAudios[i].volume = 0.0f;
        }
    }

    public void UnMuteAll()
    {
        bMuted = false;
        PausedAudios = FindObjectsOfType<AudioSource>();
        for (int i = 0; i < PausedAudios.Length; i++)
        {
            PausedAudios[i].volume = 1.0f;
        }
    }

    public void Update()
    {
        SESource.volume = (bMuted ? 0.0f : SEVolume);

        if (!bChangingBGM)
        {
            if (1.0f != CurrentVolume)
                CurrentVolume = Mathf.Min(1.0f, CurrentVolume + FadeInSpeed * Time.deltaTime);
        }
        else
        {
            CurrentVolume = CurrentVolume - FadeOutSpeed * Time.fixedDeltaTime;
            if (CurrentVolume <= 0.0f)
            {
                bChangingBGM = false;
                PlayBGM(NewBGM);
                return;
            }
        }

        BGMSource.volume = CurrentVolume * (bMuted ? 0.0f : BGMVolume);
    }
}
